package com.medata.utils;

import com.medata.exeptions.YunExceptions;
import com.medata.vo.RelationsVo;
import com.medata.vo.SimDouVo;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.*;

/**
 * @author ：zb
 * @date ：Created in 2019/5/17 0017 14:39
 * @description：
 */

public class XmlUtil {

    public static String getReplace(String str){
        if (str!=null){
            str=str.replaceAll("<","&lt;");
            str=str.replaceAll(">","&gt;");
            str=str.replaceAll("\"","&quot;");
            str=str.replaceAll("&","&amp;");
            str=str.replaceAll("'","&apos;");
        }
        return str;
    }

    public static String getUnreplace(String str){
        if (str!=null){
            str=str.replaceAll("&lt;","<");
            str=str.replaceAll("&gt;",">");
            str=str.replaceAll("&quot;","\"");
            str=str.replaceAll("&amp;","&");
            str=str.replaceAll("&apos;","'");
        }
        return str;
    }

    @SuppressWarnings("unchecked")
    public static List<String> getValues2(String xml, Integer id, Integer size) {
        try {
            List<String> values = new LinkedList<>();
            Document dom = DocumentHelper.parseText(xml);
            Element root = dom.getRootElement();
            List<Element> list = new LinkedList<>();
            list = root.elements();
            for (Element element : list) {
                if (size > 0) {
                    values.add(element.getText());
                    size--;
                }
            }
            return values;
        } catch (Exception e) {
            throw new YunExceptions(0, "解析数据库" + id + "列表的时候,出现bug");
        }
    }



    @SuppressWarnings("unchecked")
    public static Map<String,String> getValues(String xml, Integer id, Integer size) {
        try {
            Map<String,String> values = new HashMap<>();
            Document dom = DocumentHelper.parseText(xml);
            Element root = dom.getRootElement();
            List<Element> list = new LinkedList<>();
            list = root.elements();
            for (Element element : list) {
                String desc = element.attributeValue("FieldDesc");
                String value = element.getText();
                values.put(desc,value);
            }
            return values;
        } catch (Exception e) {
            throw new YunExceptions(0, "解析数据库" + id + "列表的时候,出现bug");
        }
    }

    @SuppressWarnings("unchecked")
    public static List<String> getFValues(String xml, Integer id, Integer size,String docTitle) {
        try {
            List<String> values = new LinkedList<>();
            Document dom = DocumentHelper.parseText(xml);
            Element root = dom.getRootElement();
            List<Element> list = new LinkedList<>();
            list = root.elements();
            int i=0;
            for (Element element : list) {
                if (size > 0) {
                    if (i==0){
                        values.add(docTitle);
                        size--;
                        i++;
                    }else {
                        values.add(element.getText());
                        size--;
                    }
                }
            }
            return values;
        } catch (Exception e) {
            throw new YunExceptions(0, "解析数据库" + id + "列表的时候,出现bug");
        }
    }

    @SuppressWarnings("unchecked")
    public static RelationsVo getRelations(String xml, Integer dbaid) {
        try {
            RelationsVo relationsVo = new RelationsVo();
            Document dom = DocumentHelper.parseText(xml);
            Element root = dom.getRootElement();
            List<Element> list = new ArrayList<>();
            list = root.elements();
            if (dbaid == 16) {
                relationsVo.setKname(list.get(0).getText());
                relationsVo.setKid(list.get(1).getText());
            }else if (dbaid==2){
                relationsVo.setGname(list.get(0).getText());
            }
            return relationsVo;
        } catch (Exception e) {
            throw new YunExceptions(0, "获取关联表,解析xml数据出错");
        }
    }


    @SuppressWarnings("unchecked")
    public static SimDouVo getValuesMap(String xml, Integer id, Integer size) {
        try {
            LinkedList<String> simpleValue = new LinkedList<>();
            LinkedList<String> doubleValue = new LinkedList<>();
            Document dom = DocumentHelper.parseText(xml);
            Element root = dom.getRootElement();
            List<Element> list = new LinkedList<>();
            list = root.elements();
            for (Element element : list) {
                if (size > 0) {
                    String name = element.attributeValue("InputType");
                    if (name.equals("0")||name.equals("1")){
                        String text = element.getText();
                        text=XmlUtil.getUnreplace(text);
                        simpleValue.add(text);
                        size--;
                    }else {
                        String text = element.getText();
                        String[] values = text.split(",");
                        for (String s : values) {
                            s=XmlUtil.getUnreplace(s);
                            doubleValue.add(s);
                        }
                    }
                } else {
                    String text = element.getText();
                    String[] values = text.split(",");
                    for (String s : values) {
                        s=XmlUtil.getUnreplace(s);
                        doubleValue.add(s);
                    }
                }
            }
            SimDouVo vo = new SimDouVo();
            vo.setSimpList(simpleValue);
            vo.setDoubleList(doubleValue);
            return vo;
        } catch (Exception e) {
            throw new YunExceptions(0, "解析数据库" + id + "列表的时候,出现bug");
        }
    }


}
